<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Message_class extends CI_Model {
	
	public function list_msg()
	{
		$intUserID = $this->session->userdata('intUserID');

		$this->db->select('*');
		$query = $this->db->from('messages');
		$query = $this->db->join('users', 'users.intUserID = messages.strFrom', 'left');
		$query = $this->db->order_by("messages.dateCreated", "DESC");
		$query = $this->db->where(array('messages.strTo'=>$intUserID,'messages.intDeleted'=>0));
		$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}
	}
	public function get_message($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));

		$this->db->select('messages.*, users.intUserID, users.strFirstName, users.strMiddleName, users.strLastName, users.intAccountType');
		$query = $this->db->order_by("dateCreated", "DESC");
		$query = $this->db->from('messages');
		$query = $this->db->join('users', 'users.intUserID = messages.strFrom', 'left');
		$query = $this->db->where(array('messages.intMsgID'=>$intMsgID,'messages.strTo'=>$intUserID,'messages.intDeleted'=>0));
		$query = $this->db->get()->result_array();
		$query = current($query);
		
		if($query['intMsgID']==""){
			return 0;
		} else {
			return $query;
		}
	}
	public function update_msg_status($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));
			
		$data = array(
				'intStatus' => 1
		);
		$this->db->where(array('intMsgID'=>$intMsgID,'strTo'=>$intUserID));
		$this->db->update('messages', $data);
	
		if($this->db->_error_number()==0)
		{
			$data['intStatus'] = 1; // Success
		}
		else
		{
			$data['intStatus'] = 0; // Failed
		}
		return $data;
	}
	public function count_new_msg()
	{
		$intUserID = $this->session->userdata('intUserID');

		$this->db->select('*');
		$query = $this->db->where(array('strTo'=>$intUserID,'intStatus'=>0,'intDeleted'=>0));
		$query = $this->db->count_all_results('messages');
		
		return $query;
	}
	public function send_message($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$strTo = stripslashes(filter_var($post['strTo'], FILTER_SANITIZE_STRING));
		$strMsgBody = stripslashes(filter_var($post['strMessage'], FILTER_SANITIZE_STRING));
		
		$data = array(
				'strFrom' => $intUserID,
				'strTo' => $strTo,
				'strMsgBody' => $strMsgBody,
				'intStatus' => 0,
				'intDeleted' => 0,
				'dateCreated'=> date('Y-m-d H:i:s')
		);
				
		$this->db->insert('messages', $data);
		
		if($this->db->_error_number()==0)
		{
			$data['intStatus'] = 0; // Success
		}
		else
		{
			$data['intStatus'] = 1; // Failed
		}
		return $data;
	}
	public function delete_msg($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));
			
		$data = array(
				'intDeleted' => 1
		);
		$this->db->where(array('intMsgID'=>$intMsgID,'strTo'=>$intUserID));
		$this->db->update('messages', $data);
	
		if($this->db->_error_number()==0)
		{
			$data['intStatus'] = 1; // Success
		}
		else
		{
			$data['intStatus'] = 0; // Failed
		}
		return $data;
	}
	public function list_sent_msg()
	{
		$intUserID = $this->session->userdata('intUserID');

		$this->db->select('*');
		$query = $this->db->from('messages');
		$query = $this->db->join('users', 'users.intUserID = messages.strTo', 'left');
		$query = $this->db->order_by("messages.dateCreated", "DESC");
		$query = $this->db->where(array('messages.strFrom'=>$intUserID,'messages.strSentItem'=>0,'messages.intDeleted'=>0));
		$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}
	}
	public function get_sent_message($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));

		$this->db->select('messages.*, users.intUserID, users.strFirstName, users.strMiddleName, users.strLastName, users.intAccountType');
		$query = $this->db->order_by("dateCreated", "DESC");
		$query = $this->db->from('messages');
		$query = $this->db->join('users', 'users.intUserID = messages.strTo', 'left');
		$query = $this->db->where(array('messages.intMsgID'=>$intMsgID,'messages.strFrom'=>$intUserID,'messages.intDeleted'=>0));
		$query = $this->db->get()->result_array();
		$query = current($query);
		
		if($query['intMsgID']==""){
			return 0;
		} else {
			return $query;
		}
	}
	public function delete_sent_msg($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));
			
		$data = array(
				'strSentItem' => 1
		);
		$this->db->where(array('intMsgID'=>$intMsgID,'strFrom'=>$intUserID));
		$this->db->update('messages', $data);
	
		if($this->db->_error_number()==0)
		{
			$data['intStatus'] = 1; // Success
		}
		else
		{
			$data['intStatus'] = 0; // Failed
		}
		return $data;
	}
	public function message_trash()
	{
		$intUserID = $this->session->userdata('intUserID');

		$this->db->select('*');
		$query = $this->db->from('messages');
		$query = $this->db->join('users', 'users.intUserID = messages.strFrom', 'left');
		$query = $this->db->order_by("messages.dateCreated", "DESC");
		$query = $this->db->where(array('messages.strTo'=>$intUserID,'messages.intDeleted'=>1));
		$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}
	}
	public function get_trash_message($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));

		$this->db->select('messages.*, users.intUserID, users.strFirstName, users.strMiddleName, users.strLastName, users.intAccountType');
		$query = $this->db->order_by("dateCreated", "DESC");
		$query = $this->db->from('messages');
		$query = $this->db->join('users', 'users.intUserID = messages.strTo', 'left');
		$query = $this->db->where(array('messages.intMsgID'=>$intMsgID,'messages.intDeleted'=>1));
		$query = $this->db->get()->result_array();
		$query = current($query);
		
		if($query['intMsgID']==""){
			return 0;
		} else {
			return $query;
		}
	}
	public function delete_trash_msg($post)
	{
		$intUserID = $this->session->userdata('intUserID');
		$intMsgID = stripslashes(filter_var($post['intMsgID'], FILTER_SANITIZE_STRING));
			
		$data = array(
				'intDeleted' => 2
		);
		$this->db->where(array('intMsgID'=>$intMsgID,'strFrom'=>$intUserID));
		$this->db->update('messages', $data);
	
		if($this->db->_error_number()==0)
		{
			$data['intStatus'] = 1; // Success
		}
		else
		{
			$data['intStatus'] = 0; // Failed
		}
		return $data;
	}
}